import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useCounterStore = defineStore('cart', () => {
   let carts = ref([])
   function addCart(detailData,goods_id){
      let username = localStorage.getItem('username');
      let usercart = carts.value.filter(item => item.username == username)
      let pdcart = usercart.find(item => item.goods.goods_id == goods_id)
      if(pdcart){
        pdcart.number++
      }else{
        carts.value.push({
            username:username,
            id:Math.floor(Math.random() * 10000000),
            goods:detailData,
            checked:true,
            number:1
        })
      }
   }
    return { carts,addCart }
},
    {
        persist: true,
    },
)